<template>
  <el-dialog
    title="选择"
    :visible.sync="visible"
    :before-close="close"
    append-to-body
    :close-on-click-modal="false"
    width="40%">
    <el-form label-position="top">
      <el-form-item label="发起车间(单选)">
        <el-select v-model="selectWorkshop" value-key="workshopId" filterable v-if="workshops.length" placeholder="发起车间" style="width: 100%">
          <el-option
            v-for="item in workshops"
            :key="item.workshopId"
            :label="item.workshopName"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="测试菌种(可多选)" v-if="selectWorkshop != null">
        <el-checkbox-group v-model="selectEnvGetObjects" @change="changeEnvGetObjects">
          <el-checkbox-button v-for="item in envGetObjects" :label="item" :key="item.envGetObjectId">{{item.envGetObjectName}}</el-checkbox-button>
        </el-checkbox-group>
      </el-form-item>
      <template v-for="(obj, objIndex) in selectEnvGetObjects">
        <el-form-item :key="objIndex" :label="obj.envGetObjectName+'(可多选)'">
          <el-checkbox-group v-model="obj.selectFrequencys">
            <el-checkbox-button v-for="item in envGetFrequencys" :label="item" :key="item.envGetFrequencyId">{{item.envGetFrequencyName}}</el-checkbox-button>
          </el-checkbox-group>
        </el-form-item>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button class="pan-btn primary-btn" type="primary" @click="ok">下一步</el-button>
  	</span>
  </el-dialog>
</template>

<script>
  import { isBlank,arrCompareByProp } from '@/common/js/utils'
  import { getWorkshopList,getEnvGetObjectList,getEnvGetFrequencyList } from "@/api/workshop"
  import { getVersion } from "@/api/check"
  import { CODE_OK } from '@/common/js/config'
  export default {
    data() {
    	return {
    		visible: false,
        workshops: [],
        envGetObjects: [],
        envGetFrequencys: [],
        selectWorkshop: null,
        selectEnvGetObjects: [],
        versions: []
    	}
    },
    created() {
      this.visible = true
    	this.getInfo()
    },
    methods: {
      getInfo() {
        const params1 = {
        	page: 1,
          limit: 99
        }
        getWorkshopList(params1).then(res => {
        	if (res.code === CODE_OK) {
        		this.workshops = res.page.list
        	}else{
        		this.$message.error(res.msg)
        	}
        })
        getEnvGetObjectList().then(res => {
          if (res.code === CODE_OK) {
            this.envGetObjects = res.envGetObjectList.map(v => {
              this.$set(v, 'selectFrequencys', [])
              return v
            })
          }else{
            this.$message.error(res.msg)
          }
        })
        getEnvGetFrequencyList().then(res => {
          if (res.code === CODE_OK) {
            this.envGetFrequencys = res.envGetFrequencyList
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      changeEnvGetObjects() {
        if (this.selectEnvGetObjects.length == 0) {
          this.$message.error('请选择至少一个菌种')
          return
        }
        const data = {
          checkTypeId: 8,
          workshopId: this.selectWorkshop.workshopId,
          envGetObjectIds: this.selectEnvGetObjects.map(o => {
            return o.envGetObjectId
          }),
          printClassify: '环境监测测试记录'
        }
        getVersion(data).then(res => {
          if (res.code === CODE_OK) {
            this.versions = res.list.filter(l => l.isCurrent == 0)
            if (this.versions.length != this.selectEnvGetObjects.length) {
              this.$message.error('文件版本号和对应的菌种不匹配，请联系管理员进行增加或修改')
              return
            }
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      close(val) {
      	this.$emit('close')
        if (val === 'ok') {
          let w = this.selectWorkshop
          let os = this.selectEnvGetObjects
          for (let i=0,j=os.length; i<j; i++) {
            let o = JSON.parse(JSON.stringify(os[i]))
            let fs = JSON.parse(JSON.stringify(o.selectFrequencys))
            for (let x=0,y=fs.length; x<y; x++) {
              let f = JSON.parse(JSON.stringify(fs[x]))
              this.$set(f,'envTitle',w.workshopName+o.envGetObjectName+'测试记录')
              this.$set(f,'workshopId',w.workshopId)
              this.$set(f,'workshopName',w.workshopName)
              this.$set(f,'envGetObjectId',o.envGetObjectId)
              this.$set(f,'envGetObjectName',o.envGetObjectName)
              //查找
              this.$set(f,'recordNum', o.version.recordNum)
              this.$set(f,'versionNum', o.version.versionNum)
              this.$set(f,'systemNum', o.version.systemNum)
              //未用，用的话得从数据库中获取
              this.$set(f,'standardNum','ZL·T / SOP·007')
              fs[x] = JSON.parse(JSON.stringify(f))
            }
            fs = fs.sort(arrCompareByProp("envGetFrequencyId"))
            this.$set(o,'envLists',fs)
            os[i] = JSON.parse(JSON.stringify(o))
          }
          os = os.sort(arrCompareByProp("envGetObjectId"))
          this.$emit('getSelectInfo', {
            envObjects: os
          })
        }
      	this.visible = false
      },
      ok() {
        if (this.formVerify()) {
          this.close('ok')
        }
      },
      formVerify() {
        if (this.selectWorkshop.workshopId == undefined) {
          this.$message.error('请选择发起车间')
          return false
        }
        for (let i=0,j=this.selectEnvGetObjects.length; i<j; i++) {
          let obj = JSON.parse(JSON.stringify(this.selectEnvGetObjects[i]))
          if (obj.selectFrequencys == [] || obj.selectFrequencys == null || obj.selectFrequencys == '') {
            this.$message.error('请选择'+obj.envGetObjectName+'的时间频率')
            return false
          }
          let v = this.versions.find(v => v.envGetObjectId == obj.envGetObjectId)
          if (isBlank(v)) {
            this.$message.error('未找到【'+this.selectWorkshop.workshopName+'】'+obj.envGetObjectName+'的文件版本号，请向管理员提供后再试')
            return false
          }else{
            this.$set(obj, 'version', JSON.parse(JSON.stringify(v)))
          }
          this.selectEnvGetObjects[i] = obj
        }
        return true
      }
    }
  }
</script>

<style>
</style>
